package org.flynenu.ums.common.bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeansManager {

	private final static String confFilePath = "org/flynenu/ums/common/bean/umsspringconf.xml";

	private BeanFactory beanFactory = null;

	private static BeansManager instance = null;

	private BeansManager() {
		beanFactory = new ClassPathXmlApplicationContext(confFilePath);
	}

	public String getConfFilePath() {
		return confFilePath;
	}

	public static BeansManager instance() {
		if (instance == null) {
			synchronized (BeansManager.class) {
				if (instance == null) {
					instance = new BeansManager();
				}
			}
		}

		return instance;
	}

	@SuppressWarnings("unchecked")
	public Object generateBean(BeansEnum bean) {
		Object obj = generateBeanFactory().getBean(bean.getValue());
		
		return obj;
	}
	
	public<T> T generateBean(BeansEnum bean, Class clas) {
		T obj = (T) generateBeanFactory().getBean(bean.getValue(), clas);
		
		return obj;
	}

	private BeanFactory generateBeanFactory() {
		if (beanFactory == null) {
			synchronized (BeansManager.class) {
				if (beanFactory == null) {
					beanFactory = new ClassPathXmlApplicationContext(confFilePath);
				}
			}
		}

		return beanFactory;
	}

}
